package cc.sjfx.sharedoc.db;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import cc.sjfx.sharedoc.bean.ExamShowListViewBean;
import cc.sjfx.sharedoc.cache.UserCache;
import cc.sjfx.sharedoc.db.bmob.Examination;

import com.activeandroid.ActiveAndroid;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Select;

@Table(name = "Exam")
public class ExamDB extends Model {
	@Column(name = "ObjectID", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
	public String objectId;
	@Column(name = "DownloadUrl")
	public String downloadUrl;
	@Column(name = "Content")
	public String content;
	@Column(name = "Major")
	public int major;
	@Column(name = "School")
	public int school;
	@Column(name = "Publisher")
	public String publisher;
	@Column(name = "Title")
	public String title;
	@Column(name = "Type")
	public int type;
	@Column(name = "Year")
	public int date_year;
	@Column(name = "Month")
	public int date_month;
	@Column(name = "Day")
	public int date_day;
	@Column(name = "Date")
	public long date;
	@Column(name = "ReplyNum")
	public int replyNum;
	@Column(name = "CreatedAt")
	public String createdAt;
	@Column(name = "User")
	public UserDB user;
	@Column(name = "File")
	public String file;

	// public ExamDB(String downloadUrl, String content, int major, int school,
	// String publisher, String title,int type, int date_year, int date_month,
	// int date_day, UserDB user) {
	// super();
	// this.downloadUrl = downloadUrl;
	// this.content = content;
	// this.major = major;
	// this.school = school;
	// this.publisher = publisher;
	// this.title = title;
	// this.type=type;
	// this.date_year = date_year;
	// this.date_month = date_month;
	// this.date_day = date_day;
	// this.user = user;
	// }

	public ExamDB() {

	}

	public static long getNewestDate(int type) {
		ExamDB db;
		if (UserCache.isLogin) {
			db = new Select().from(ExamDB.class)
					.where("Major  = ?", UserCache.user.getMid())
					.where("Type  = ?", type).orderBy("Date DESC")
					.executeSingle();
		} else {
			db = new Select().from(ExamDB.class).orderBy("Date DESC")
					.where("Type  = ?", type).executeSingle();
		}
		long d = 0;
		if (db != null) {
			d = db.date;
		}
		return d;
	}

	public static List<ExamShowListViewBean> getData(int type) {
		List<ExamDB> dbs;
		if (UserCache.user != null) {
			dbs = new Select().from(ExamDB.class).orderBy("Date DESC")
					.where("Major = ?", UserCache.user.getMid())
					.where("type=?", type).execute();
		} else {
			dbs = new Select().from(ExamDB.class).orderBy("Date DESC")
					.where("type=?", type).execute();
		}
		List<ExamShowListViewBean> list = new ArrayList<ExamShowListViewBean>();
		if (dbs != null) {
			for (int i = 0; i < dbs.size(); i++) {
				list.add(db2Bean(dbs.get(i)));
			}
		}
		return list;
	}

	public static ExamShowListViewBean getSingleData(String objectId) {
		ExamDB db = new Select().from(ExamDB.class)
				.where("ObjectId = ?", objectId).executeSingle();
		return db2Bean(db);
	}

	public String getObjectId() {
		return objectId;
	}

	public void setObjectId(String objectId) {
		this.objectId = objectId;
	}

	public static void updateReplyNum(Context context, String objectId,
			int count) {
		ExamDB db = new Select().from(ExamDB.class)
				.where("ObjectID  = ?", objectId).executeSingle();
		db.replyNum = count;
		db.save();
	}

	public static ExamShowListViewBean db2Bean(ExamDB db) {
		ExamShowListViewBean bean = new ExamShowListViewBean(db.title,
				db.content, db.downloadUrl, db.replyNum, db.file, db.user,
				db.publisher, db.date_year + "年" + db.date_month + "月"
						+ db.date_day + "日", db.date_year, db.date_month,
				db.date_day, db.createdAt, db.objectId);
		return bean;
	}

	public static int setData(Context context, List<Examination> list, int type) {
		int count = 0;
		long d = getNewestDate(type);
		ActiveAndroid.beginTransaction();
		try {
			Examination e;
			for (int i = list.size() - 1; i >= 0; i--) {
				e = list.get(i);
				long da = e.getDate();
				if (da > d) {
					count++;
					ExamDB examDB = new ExamDB();
					examDB.downloadUrl = e.getDownloadUrl();
					examDB.content = e.getContent();
					examDB.major = e.getMajor();
					examDB.school = e.getSchool();
					examDB.publisher = e.getPublisher();
					examDB.title = e.getTitle();
					examDB.type = e.getType();
					examDB.date_year = e.getDate_year();
					examDB.date_month = e.getDate_month();
					examDB.date_day = e.getDate_day();
					examDB.date = da;
					examDB.replyNum = e.getReplyNum();
					examDB.createdAt = e.getCreatedAt();
					examDB.user = UserDB.getUserDB(context, e.getUser());
					examDB.objectId = e.getObjectId();
					try {
						if (e.getFileJson() != null) {
							examDB.file = e.getFileJson().toString();
						}
					} catch (JSONException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					examDB.save();
				}
			}
			ActiveAndroid.setTransactionSuccessful();
		} finally {
			ActiveAndroid.endTransaction();
		}
		return count;
	}
}
